package com.yy.wga.goods.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.goods.feign.request.SharerProductSetCommissionReq;
import com.yy.wga.goods.feign.request.SharerSetCommissionReq;
import com.yy.wga.goods.service.api.PromoterApi;
import com.yy.wga.goods.service.api.request.bind.GetPromoterRegisterAndBindStatusReq;
import com.yy.wga.goods.service.api.request.commission.GetSharerProductCommissionReq;
import com.yy.wga.goods.service.api.request.commission.SetSharerCommissionReq;
import com.yy.wga.goods.service.api.request.commission.SetSharerProductCommissionReq;
import com.yy.wga.goods.service.api.request.feed.GetFeedListReq;
import com.yy.wga.goods.service.api.request.feed.GetFeedPromotionInfoReq;
import com.yy.wga.goods.service.api.request.live.GetLiveCommissionProductListReq;
import com.yy.wga.goods.service.api.request.live.GetLiveNoticeRecordListReq;
import com.yy.wga.goods.service.api.request.live.GetLiveNoticeRecordQrCodeReq;
import com.yy.wga.goods.service.api.request.live.GetLiveRecordListReq;
import com.yy.wga.goods.service.api.request.live.GetLiveRecordQrCodeReq;
import com.yy.wga.goods.service.api.request.telent.GetBindTalentListReq;
import com.yy.wga.goods.service.api.response.ApiResult;
import com.yy.wga.goods.service.api.response.BaseApiDTO;
import com.yy.wga.goods.service.api.response.bind.BindTalentListResp;
import com.yy.wga.goods.service.api.response.bind.GetPromoterRegisterAndBindStatusResp;
import com.yy.wga.goods.service.api.response.commission.SharerProductCommissionResp;
import com.yy.wga.goods.service.api.response.feed.FeedListResp;
import com.yy.wga.goods.service.api.response.feed.FeedPromotionInfoResp;
import com.yy.wga.goods.service.api.response.live.LiveCommissionProductListResp;
import com.yy.wga.goods.service.api.response.live.LiveNoticeRecordListResp;
import com.yy.wga.goods.service.api.response.live.LiveNoticeRecordQrCodeResp;
import com.yy.wga.goods.service.api.response.live.LiveRecordListResp;
import com.yy.wga.goods.service.api.response.live.LiveRecordQrCodeResp;
import com.yy.wga.goods.service.service.PromoterService;
import com.yy.wga.wechat.feign.api.WechatMpFeign;
import com.yy.wga.wechat.feign.response.WechatGetAccessTokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class PromoterServiceImpl implements PromoterService {

    @Resource
    private PromoterApi promoterApi;
    @Resource
    private WechatMpFeign wechatMpFeign;

    /**
     * 是否走简易版本注册
     * 【当走简易版本注册时，可以不要求推客开通商户号，后面分佣只能走机构自己分佣】
     */
    private static final Boolean IS_SIMPLE_REGISTER = true;


    public GetPromoterRegisterAndBindStatusResp getPromoterRegisterAndBindStatus(String openId) {
        GetPromoterRegisterAndBindStatusReq registerAndBindStatusReq = new GetPromoterRegisterAndBindStatusReq();
        registerAndBindStatusReq.setSharerOpenid(openId);
        registerAndBindStatusReq.setIsSimpleRegister(IS_SIMPLE_REGISTER);
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getPromoterRegisterAndBindStatus req:{}", JSONObject.toJSONString(registerAndBindStatusReq));
        JSONObject jsonObject = promoterApi.getPromoterRegisterAndBindStatus(accessToken, registerAndBindStatusReq);
        log.info("invoke getPromoterRegisterAndBindStatus resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, GetPromoterRegisterAndBindStatusResp.class);
    }


    @Override
    public BaseApiDTO setSharerCommission(SharerSetCommissionReq req) {
        SetSharerCommissionReq setSharerCommissionReq = new SetSharerCommissionReq();
        setSharerCommissionReq.setCommissionType(req.getCommissionType());
        setSharerCommissionReq.setCommissionRatio(req.getCommissionRatio());
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        setSharerCommissionReq.setSharerAppid(getAccessTokenResponse.getAppId());
        log.info("invoke setSharerCommission req:{}", JSONObject.toJSONString(setSharerCommissionReq));
        JSONObject jsonObject = promoterApi.setSharerCommission(accessToken, setSharerCommissionReq);
        log.info("invoke setSharerCommission resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, BaseApiDTO.class);
    }


    @Override
    public BaseApiDTO setSharerProductCommission(SharerProductSetCommissionReq req) {
        SetSharerProductCommissionReq setSharerCommissionReq = new SetSharerProductCommissionReq();
        setSharerCommissionReq.setProductId(req.getSpuId());
        setSharerCommissionReq.setCommissionRatio(req.getCommissionRatio());
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        setSharerCommissionReq.setSharerAppid(getAccessTokenResponse.getAppId());
        log.info("invoke setSharerCommission req:{}", JSONObject.toJSONString(setSharerCommissionReq));
        JSONObject jsonObject = promoterApi.setSharerProductCommission(accessToken, setSharerCommissionReq);
        log.info("invoke setSharerCommission resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, BaseApiDTO.class);
    }

    @Override
    public SharerProductCommissionResp getSharerProductCommission(String spuId) {
        GetSharerProductCommissionReq getSharerProductCommissionReq = new GetSharerProductCommissionReq();
        getSharerProductCommissionReq.setProductId(spuId);
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        getSharerProductCommissionReq.setSharerAppid(getAccessTokenResponse.getAppId());
        log.info("invoke getSharerProductCommission req:{}", JSONObject.toJSONString(getSharerProductCommissionReq));
        JSONObject jsonObject = promoterApi.getSharerProductCommission(accessToken, getSharerProductCommissionReq);
        log.info("invoke getSharerProductCommission resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, SharerProductCommissionResp.class);
    }


    @Override
    public BindTalentListResp getBindTalentList(GetBindTalentListReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getBindTalentList req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getBindTalentList(accessToken, req);
        log.info("invoke getBindTalentList resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, BindTalentListResp.class);
    }

    @Override
    public LiveRecordListResp getLiveRecordList(GetLiveRecordListReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getLiveRecordList req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getLiveRecordList(accessToken, req);
        log.info("invoke getLiveRecordList resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, LiveRecordListResp.class);
    }

    @Override
    public LiveNoticeRecordListResp getLiveNoticeRecordList(GetLiveNoticeRecordListReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getLiveNoticeRecordList req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getLiveNoticeRecordList(accessToken, req);
        log.info("invoke getLiveNoticeRecordList resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, LiveNoticeRecordListResp.class);
    }

    @Override
    public LiveNoticeRecordQrCodeResp getLiveNoticeRecordQrCode(GetLiveNoticeRecordQrCodeReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getLiveNoticeRecordQrCode req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getLiveNoticeRecordQrCode(accessToken, req);
        log.info("invoke getLiveNoticeRecordQrCode resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, LiveNoticeRecordQrCodeResp.class);
    }

    @Override
    public LiveRecordQrCodeResp getLiveRecordQrCode(GetLiveRecordQrCodeReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getLiveRecordQrCode req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getLiveRecordQrCode(accessToken, req);
        log.info("invoke getLiveRecordQrCode resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, LiveRecordQrCodeResp.class);
    }

    @Override
    public LiveCommissionProductListResp getLiveCommissionProductList(GetLiveCommissionProductListReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getLiveCommissionProductList req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getLiveCommissionProductList(accessToken, req);
        log.info("invoke getLiveCommissionProductList resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, LiveCommissionProductListResp.class);
    }

    @Override
    public FeedListResp getFeedList(GetFeedListReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getFeedList req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getFeedList(accessToken, req);
        log.info("invoke getFeedList resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, FeedListResp.class);
    }

    @Override
    public FeedPromotionInfoResp getFeedPromotionInfo(GetFeedPromotionInfoReq req) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();
        log.info("invoke getFeedPromotionInfo req:{}", JSONObject.toJSONString(req));
        JSONObject jsonObject = promoterApi.getFeedPromotionInfo(accessToken, req);
        log.info("invoke getFeedPromotionInfo resp:{}", JSONObject.toJSONString(jsonObject));
        return ApiResult.handle(jsonObject, FeedPromotionInfoResp.class);
    }
}
